package com.ruoyi.discuss.service.impl;

import java.util.List;

import com.ruoyi.discuss.domain.Bigcourse;
import com.ruoyi.discuss.domain.Biglike;
import com.ruoyi.discuss.domain.Bigtopic;
import com.ruoyi.discuss.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.discuss.mapper.BigreplyMapper;
import com.ruoyi.discuss.domain.Bigreply;
import org.springframework.transaction.annotation.Transactional;

/**
 * 回复Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@Service
public class BigreplyServiceImpl implements IBigreplyService
{
    @Autowired
    private BigreplyMapper bigreplyMapper;

    @Autowired
    private IBigcourseService bigcourseService;

    @Autowired
    private IBigtopicService bigtopicService;

    @Autowired
    private IBiglikeService biglikeService;

    @Autowired
    private IBiguserService biguserService;

    /**
     * 查询回复
     *
     * @param replyid 回复主键
     * @return 回复
     */
    @Override
    public Bigreply selectBigreplyByReplyid(Long replyid)
    {
        return bigreplyMapper.selectBigreplyByReplyid(replyid);
    }

    /**
     * 查询回复列表
     *
     * @param bigreply 回复
     * @return 回复
     */
    @Override
    public List<Bigreply> selectBigreplyList(Bigreply bigreply)
    {
        return bigreplyMapper.selectBigreplyList(bigreply);
    }

    /**
     * 新增回复
     *
     * @param bigreply 回复
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBigreply(Bigreply bigreply)
    {
        //根据传过来的话题获取课程
        Long topicid = bigreply.getTopicid();
        Bigtopic bigtopic = bigtopicService.selectBigtopicByTopicid(topicid);
        String subject = bigtopic.getSubject();
        Bigcourse bigcourse = bigcourseService.selectBySubject(subject);
        biguserService.updatePointById(bigreply.getUserid(), bigcourse.getAddpoint());
        return bigreplyMapper.insertBigreply(bigreply);
    }

    /**
     * 修改回复
     *
     * @param bigreply 回复
     * @return 结果
     */
    @Override
    public int updateBigreply(Bigreply bigreply)
    {
        return bigreplyMapper.updateBigreply(bigreply);
    }

    /**
     * 批量删除回复
     *
     * @param replyids 需要删除的回复主键
     * @return 结果
     */
    @Override
    public int deleteBigreplyByReplyids(Long[] replyids)
    {
        return bigreplyMapper.deleteBigreplyByReplyids(replyids);
    }

    /**
     * 删除回复信息
     *
     * @param replyid 回复主键
     * @return 结果
     */
    @Override
    public int deleteBigreplyByReplyid(Long replyid)
    {
        return bigreplyMapper.deleteBigreplyByReplyid(replyid);
    }

    @Transactional
    @Override
    public int addPoint(Bigreply bigreply) {
        Biglike biglike = new Biglike();
        bigreply.setLikescount(bigreply.getLikescount()+1);
        biglike.setReplyid(bigreply.getReplyid());
        biglikeService.insertBiglike(biglike);
        int i = this.updateBigreply(bigreply);
        return i;
    }
}
